#!/usr/bin/perl
# mysqldump2email
#
# $Id: mysqldump2email 324 2006-07-23 06:09:46Z ogawa $

use strict;
package mysqldump2email;
use YAML;
use MIME::Lite;
use Encode;
use DateTime;
use File::Spec;
use Pod::Usage;
use Getopt::Long;

our $VERSION = '0.01';

my %opt = (conf => 'config.yaml');
GetOptions(\%opt, 'help', 'conf=s') or pod2usage(2);
pod2usage(1) if $opt{help};

my $cfg = YAML::LoadFile($opt{conf});

my $dt = DateTime->now;
$dt->set_time_zone($cfg->{time_zone} || 'local');

my $zipfile = dump_mysql($cfg, $dt);
my $cfg_mail = $cfg->{mail}
    or die 'No "mail" setting found in config.yaml';

my $mailroute = $cfg_mail->{route} || { via => 'smtp', host => 'localhost' };
MIME::Lite->send($mailroute->{via},
		 $mailroute->{host} ? ($mailroute->{host}) : ());

my $rfc822date = $dt->strftime('%a, %d %b %Y %H:%M:%S %z');
my $msg = MIME::Lite->new(
    Date => $rfc822date,
    From => __PACKAGE__ . ' <' . $cfg_mail->{from} . '>',
    To => $cfg_mail->{to},
    Subject => encode('MIME-Header', __PACKAGE__),
    Type => 'multipart/mixed',
);
$msg->attach(
    Type => 'TEXT',
    Data => 'Dumped by ' . __PACKAGE__ . ', at ' . $rfc822date,
);
$msg->attach(
    Type => 'application/zip',
    Path => $zipfile,
    Filename => __PACKAGE__ . '.zip',
    Disposition => 'attachment',
);
$msg->send();
unlink($zipfile);

sub dump_mysql {
    my ($cfg, $dt) = @_;
    my $cfg_mdump = $cfg->{mysqldump}
	or die 'No "mysqldump" setting found in config.yaml';
    my $cfg_zip = $cfg->{zip}
	or die 'No "zip" setting found in config.yaml';

    my $ts = $dt->strftime('%Y%m%d%H%M%S');
    my $tmpdir = $cfg->{'tmpdir'} || '/tmp';
    my $fname = __PACKAGE__ . '.' . $ts;
    my $dumpfile = File::Spec->catfile($tmpdir, $fname);
    my $zipfile = $dumpfile . '.zip';

    system($cfg_mdump->{command} || 'mysqldump',
	   '--all-databases',
	   ($cfg_mdump->{username} ? ('--user=' . $cfg_mdump->{username}) : ()),
	   ($cfg_mdump->{password} ? ('--password=' . $cfg_mdump->{password}) : ()),
	   ($cfg_mdump->{host} ? ('--host=' . $cfg_mdump->{host}) : ()),
	   '--result-file=' . $dumpfile) == 0
	or die 'Failed to execute "mysqldump" command';

    system($cfg_zip->{command} || 'zip',
	   '-9mqj',
	   ($cfg_zip->{password} ? ('-P', $cfg_zip->{password}) : ()),
	   $zipfile, $dumpfile) == 0
	or die 'Failed to execute "zip" command';

    $zipfile;
}

=head1 NAME

mysqldump2email - Dump MySQL data and send it to your mail server.

=head1 SYNOPSIS

  % mysqldump2email
  % mysqldump2email --conf /path/to/config.yaml

=head1 DESCRIPTION

C<mysqldump2email> is a command line application that dumps MySQL
databases and zip the dump file, and then sends it to your mail
server.  It allows you to easily backup your MySQL databases into your
mail spool, typically into your Google Mail spool.

You can run this via crontab, for example, everyday.

=head1 REQUIREMENT

This application requires perl 5.8.0 with following Perl modules
installed on your box.

=over 4

=item DateTime

=item DateTime::Format:Mail

=item YAML

=back

=head1 OPTIONS

This application has a command-line option as follows:

=over 4

=item --conf /path/to/config.yaml

Specifies the path to a configuration file. By default, C<config.yaml>
in the current directory.

=back

=head1 CONFIGURATION

The distributions includes a sample configuration file
C<config.yaml.sample>. You can rename it to C<config.yaml> and
configure C<mysqldump2email>.

=over 4

=item mysqldump:

=over 4

=item command

Set the full path of C<mysqldump> command in your box.

=item username, password

Set your username and password for MySQL.

=item host (Optional)

Set your hostname of MySQL server.

=back

=item zip:

=over 4

=item command

Set the full path of C<zip> command in your box.

=item password (Optional)

Set the password for encrypting/decrypting zip files.  If not set, no
encryption is performed.

=back

=item mail:

=over 4

=item from

Set email address for this application, which is used as C<From:> header.

=item to

Set the target email address, which is sent emails to.

=item route

Set how to send emails. Default is to use SMTP.

=back

=item tmpdir:

Set the temporary directory for this application.

=item time_zone: (Optional)

Sets the timezone for outgoing email C<Date:> header.  Default is set
to use local timezone of your box.

=back

=head1 DEVELOPMENT

The development version is always available from the following
subversion repository:

http://code.as-is.net/svn/public/mysqldump2email/trunk

You can browse the files via viewvc from the following:

http://code.as-is.net/viewvc/public/browse/mysqldump2email/trunk/

Any comments, suggestions, or patches are welcome.

=head1 AUTHOR

Hirotaka Ogawa E<lt>hirotaka.ogawa at gmail.comE<gt>

This script is free software and licensed under the same terms as Perl
(Artistic/GPL).

=cut
